<template>
  <el-dialog :title="info.title" v-if="visible" :visible.sync="visible" center width="1160px" :before-close="handleCancel">
    <div class="">
      <div class="page-filter mb10 fl">
        <el-form ref="filter" :model="filter" label-width="0">
          <el-form-item class="mr10 mb5 fl">
            <el-input v-model="filter.name" placeholder="流程名称" size="small" clearable />
          </el-form-item>
          <el-form-item class="mr10 mb5 fl" style="width: 300px">
            <el-button class="fl" type="primary" size="small" @click="getList">查 询</el-button>
            <el-button class="fl" type="primary" size="small" @click="handleAdd">新 增</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table ref="multipleTable" class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
        <el-table-column label="序号" align="center" width="56px">
          <template slot-scope="{$index}">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="流程名称" min-width="120px" prop="name" />
        <el-table-column label="下个流程" min-width="120px" prop="next_name" />
        <el-table-column label="上级主管" align="center" width="90px" :formatter="formatterS" />
        <el-table-column label="审批部门" width="120px" prop="dept_name" />
        <el-table-column label="审批职位" width="120px" prop="pos_name" />
        <el-table-column label="起始节点" align="center" width="90px" :formatter="formatterB" />
        <el-table-column label="结束节点" align="center" width="90px" :formatter="formatterE" />
        <el-table-column label="操作" align="center" width="110px" fixed="right">
          <template slot-scope="{row}">
            <el-dropdown style="height:29px">
              <el-button size="mini" type="primary">
                操作<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(row)">编 辑</el-dropdown-item>
                <el-dropdown-item @click.native="handleDelete(row)">删 除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
      <Dialog ref="dialog" :get-data="getList" :info="dialog" />
    </div>
  </el-dialog>
</template>

<script>
import { processInfos, delProcessInfos } from '@/api/process'
import Pagination from '@/components/Pagination'
import Dialog from './components/info-dialog'

export default {
  components: { Pagination, Dialog },
  inject: ['app'],
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      paytypes: [],
      filter: {
        pageNumber: 10,
        pageCurrent: 1,
        pageAll: 1
      },
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      list: [],
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'list', [])
        this.$set(this, 'form', this.info.edit)
        this.getList()
        this.$nextTick(() => {
          this.$refs.multipleTable.doLayout()
        })
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    getList(e) {
      processInfos({
        ...this.filter,
        ...e,
        ...{lap_id: this.form.lap_id}
      }).then(response => {
        this.list = response.list
        this.filter = response.filter
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    handleDelete(e) {
      this.$confirm('此操作将删除数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        delProcessInfos({
          ids: e['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    formatterS(e) {
      return this.formatter(e.senior_role)
    },
    formatterB(e) {
      return this.formatter(e.begin_node)
    },
    formatterE(e) {
      return this.formatter(e.end_node)
    },
    formatter(k) {
      let state = '-'
      switch (k) {
        case 0:
          state = '否'
          break;
        case 1:
          state = '是'
          break;     
        default:
          break;
      }
      return state
    },
    handleEdit(e) {
      this.dialog.title = '编辑审批流程'
      this.dialog.type = 'edit'
      e.lap_id = this.form.lap_id
      this.app.dialogParam(this.dialog.edit, ['id', 'name', 'lap_id', 'next_id', 'senior_role', 'dept_id', 'pos_id', 'begin_node', 'end_node'], e)
      this.$refs['dialog'].visible = true
    },
    handleAdd(e) {
      this.dialog.title = '新增审批流程'
      this.dialog.type = 'add'
      e = {}
      e.lap_id = this.form.lap_id
      this.app.dialogParam(this.dialog.edit, ['id', 'name', 'lap_id', 'next_id', 'senior_role', 'dept_id', 'pos_id', 'begin_node', 'end_node'], e)
      this.$refs['dialog'].visible = true
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style scoped>
</style>
